/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.flan.stock.view;

import com.flan.stock.R;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * 字体切换对话框
 * @author flan
 * @since  2016年4月8日
 */
public class FontDialog extends Dialog {

	public FontDialog(Context context) {
		super(context);
	}

	public FontDialog(Context context, int theme) {
		super(context, theme);
	}
	
	public static class Builder{
		
		private Context context;
		//private View contentView;
		private String title;
		private int checkedItem = 0;
		private boolean isSingleChoice = false;
		//private CharSequence[] items;
		private String positiveButtonText;
		private String negativeButtonText;
		private DialogInterface.OnClickListener singleChoiceClickListener;
		private DialogInterface.OnClickListener positiveButtonClickListener;
		private DialogInterface.OnClickListener negativeButtonClickListener;
		
		public Builder(Context context) {
			this.context = context;
		}
		
		public Builder setContentView(View v) {
			//this.contentView = v;
			return this;
		}
		
		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}
		
		public Builder setPositiveButton(int positiveButtonText,DialogInterface.OnClickListener listener){
			this.positiveButtonText = (String) context.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;
			return this;
		}
		
		public Builder setPositiveButton(String positiveButtonText,DialogInterface.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}
		
		public Builder setNegativeButton(int negativeButtonText,DialogInterface.OnClickListener listener) {
			this.negativeButtonText = (String) context.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(String negativeButtonText,DialogInterface.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}
		
		public Builder setSingleChoiceItems(int itemsId, int checkedItem,final OnClickListener listener) {
            //items = context.getResources().getTextArray(itemsId);
            singleChoiceClickListener = listener;
            this.checkedItem = checkedItem;
            this.isSingleChoice = true;
            return this;
        }
		
		@SuppressLint("InflateParams")
		public FontDialog create(){
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			final FontDialog dialog = new FontDialog(context,R.style.FontDialog);
			View layout = inflater.inflate(R.layout.layout_dialog_font, null);
			dialog.setContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			
			((TextView) layout.findViewById(R.id.title)).setText(title);//设置标题
			
			if(positiveButtonText != null){
				TextView ptv = (TextView) layout.findViewById(R.id.cancle);
				ptv.setText(positiveButtonText);
				if(positiveButtonClickListener != null){
					ptv.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View view) {
							positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
						}
					});
				}
			}else {
				layout.findViewById(R.id.cancle).setVisibility(View.GONE);
			}
			
			if (negativeButtonText != null) {
				
				TextView ntv = (TextView) layout.findViewById(R.id.sure);
				if (negativeButtonClickListener != null) {
					ntv.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							negativeButtonClickListener.onClick(dialog,DialogInterface.BUTTON_NEGATIVE);
						}
					});
				}
			} else {
				layout.findViewById(R.id.sure).setVisibility(View.GONE);
			}
			
			if(isSingleChoice){
				final ImageButton butBig = (ImageButton) layout.findViewById(R.id.font_big);
				final ImageButton butNormal = (ImageButton) layout.findViewById(R.id.font_normal);
				final ImageButton butSmall = (ImageButton) layout.findViewById(R.id.font_small);
				
				butBig.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						checkedItem = 0;
						switch (checkedItem) {
							case 0:
								butBig.setImageResource(R.drawable.checkbox_checked);
								butNormal.setImageResource(R.drawable.checkbox_nomal);
								butSmall.setImageResource(R.drawable.checkbox_nomal);
								break;
							case 1:
								butBig.setImageResource(R.drawable.checkbox_nomal);
								butNormal.setImageResource(R.drawable.checkbox_checked);
								butSmall.setImageResource(R.drawable.checkbox_nomal);
								break;
							case 2:
								butBig.setImageResource(R.drawable.checkbox_nomal);
								butNormal.setImageResource(R.drawable.checkbox_nomal);
								butSmall.setImageResource(R.drawable.checkbox_checked);
								break;
			
							default:
								butBig.setImageResource(R.drawable.checkbox_checked);
								butNormal.setImageResource(R.drawable.checkbox_nomal);
								butSmall.setImageResource(R.drawable.checkbox_nomal);
								break;
						}
						singleChoiceClickListener.onClick(dialog,checkedItem);
					}
				});
				butNormal.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						checkedItem = 1;
						switch (checkedItem) {
							case 0:
								butBig.setImageResource(R.drawable.checkbox_checked);
								butNormal.setImageResource(R.drawable.checkbox_nomal);
								butSmall.setImageResource(R.drawable.checkbox_nomal);
								break;
							case 1:
								butBig.setImageResource(R.drawable.checkbox_nomal);
								butNormal.setImageResource(R.drawable.checkbox_checked);
								butSmall.setImageResource(R.drawable.checkbox_nomal);
								break;
							case 2:
								butBig.setImageResource(R.drawable.checkbox_nomal);
								butNormal.setImageResource(R.drawable.checkbox_nomal);
								butSmall.setImageResource(R.drawable.checkbox_checked);
								break;
			
							default:
								butBig.setImageResource(R.drawable.checkbox_checked);
								butNormal.setImageResource(R.drawable.checkbox_nomal);
								butSmall.setImageResource(R.drawable.checkbox_nomal);
								break;
						}
						singleChoiceClickListener.onClick(dialog,checkedItem);
					}
				});
				butSmall.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						checkedItem = 2;
						switch (checkedItem) {
							case 0:
								butBig.setImageResource(R.drawable.checkbox_checked);
								butNormal.setImageResource(R.drawable.checkbox_nomal);
								butSmall.setImageResource(R.drawable.checkbox_nomal);
								break;
							case 1:
								butBig.setImageResource(R.drawable.checkbox_nomal);
								butNormal.setImageResource(R.drawable.checkbox_checked);
								butSmall.setImageResource(R.drawable.checkbox_nomal);
								break;
							case 2:
								butBig.setImageResource(R.drawable.checkbox_nomal);
								butNormal.setImageResource(R.drawable.checkbox_nomal);
								butSmall.setImageResource(R.drawable.checkbox_checked);
								break;
			
							default:
								butBig.setImageResource(R.drawable.checkbox_checked);
								butNormal.setImageResource(R.drawable.checkbox_nomal);
								butSmall.setImageResource(R.drawable.checkbox_nomal);
								break;
						}
						singleChoiceClickListener.onClick(dialog,checkedItem);
					}
				});
				
				switch (checkedItem) {
					case 0:
						butBig.setImageResource(R.drawable.checkbox_checked);
						butNormal.setImageResource(R.drawable.checkbox_nomal);
						butSmall.setImageResource(R.drawable.checkbox_nomal);
						break;
					case 1:
						butBig.setImageResource(R.drawable.checkbox_nomal);
						butNormal.setImageResource(R.drawable.checkbox_checked);
						butSmall.setImageResource(R.drawable.checkbox_nomal);
						break;
					case 2:
						butBig.setImageResource(R.drawable.checkbox_nomal);
						butNormal.setImageResource(R.drawable.checkbox_nomal);
						butSmall.setImageResource(R.drawable.checkbox_checked);
						break;
	
					default:
						butBig.setImageResource(R.drawable.checkbox_checked);
						butNormal.setImageResource(R.drawable.checkbox_nomal);
						butSmall.setImageResource(R.drawable.checkbox_nomal);
						break;
				}
			}
			
			dialog.setContentView(layout);
			return dialog;
		}
		
	}
	
}




















